package com.lxq.web.servlet.mng;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lxq.common.DaoFactory;
import com.lxq.dao.CardDao;
import com.lxq.dao.UserCardDao;
import com.lxq.entity.Card;
import com.lxq.entity.UserCard;
import com.lxq.web.servlet.common.BaseServlet;

@WebServlet("/app/mng/usercard")
public class UserCardServlet extends BaseServlet {
	private static final long serialVersionUID = -4644039576372350651L;
	private UserCardDao userCardDao = DaoFactory.getInstance("userCardDao", UserCardDao.class);
	private CardDao cardDao = DaoFactory.getInstance("cardDao", CardDao.class);

	@Override
	public void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String phone = request.getParameter("phone");

		List<String> cardsNumber = userCardDao.findCards(phone);
		JSONObject json = new JSONObject();

		try {
			if (cardsNumber != null && cardsNumber.size() > 0) {
				List<Card> cards = new ArrayList<Card>();

				for (String cardnumber : cardsNumber) {
					cards.add(cardDao.findByCardNumber(cardnumber));
				}

				json.put("success", true);
				json.put("cards", new JSONArray(cards));

			} else {
				json.put("success", false);
				json.put("reason", "用户未绑定卡！");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
	}

	@Override
	public void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String phone = request.getParameter("phone");
		String cardnumber = request.getParameter("cardnumber");
		String alias = request.getParameter("alias");

		UserCard usercard = new UserCard();
		JSONObject json = new JSONObject();

		try {
			if (phone != null && cardnumber != null) {
				switch (cardDao.findByCardNumber(cardnumber).getCardState()) {
				case 0:
					json.put("success", false);
					json.put("reason", "卡已挂失，不能绑定！");
					break;

				case 1: {
					usercard.setPhoneNumber(phone);
					usercard.setCardNumber(cardnumber);
					if (alias == null) {
						usercard.setCardAlias("");
					} else {
						usercard.setCardAlias(alias);
					}
					userCardDao.save(usercard);
					cardDao.updateCardState(cardnumber, 2);
					json.put("success", true);
					break;
				}

				case 2:
					json.put("success", false);
					json.put("reason", "卡已是绑定状态，不能再绑定！");
					break;
					
				case 3:
					json.put("success", false);
					json.put("reason", "卡已启用，不能绑定！");
					break;

				default:
					break;
				}

			} else {
				json.put("success", false);
				json.put("reason", "卡号与手机号均不能为空！");
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
	}

	@Override
	public void doEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String phone = request.getParameter("phone");
		String cardnumber = request.getParameter("cardnumber");
		String newalias = request.getParameter("newalias");

		JSONObject json = new JSONObject();

		userCardDao.updateAlias(phone, cardnumber, newalias);

		try {
			json.put("success", true);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
	}

	@Override
	public void doDelete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String phone = request.getParameter("phone");
		String cardnumber = request.getParameter("cardnumber");

		JSONObject json = new JSONObject();
		try {
			
			if (userCardDao.findByCardNumberPhone(phone, cardnumber) != null) {
				
				userCardDao.delete(phone, cardnumber);
				cardDao.updateCardState(cardnumber, 1);
				json.put("success", true);

			} else {
				json.put("success", false);
				json.put("reason", "记录不存在！");
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
	}

}
